// pages/index/index.js

import config from './../../config';
import {
    message,
    session,
    getOpenId
} from './../../utils/helper';
import {
    getNewMessage,
    getIndexSlide,
    getArticleListNew
} from './../../services/index';
import qs from 'qs';


//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    //页面的初始数据
    data: {
        host: config.service.transfer_protocol + '://' + config.service.domain,
        imgs: [],
        article: {},
        shareQuan: true,
        news: 0
    },

    //事件处理函数
    onLoad: function() {
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        });
        let that = this;
        //获取幻灯片图片
        getIndexSlide({}, {
            success: res => {
                that.setData({
                    imgs: res.data
                });
            }
        });
        //获取最新文章列表
        getArticleListNew({}, {
            success: res => {
                that.setData({
                    article: res.data
                });
            }
        });
        //新消息
        getOpenId(openid => {
            //获取幻灯片图片
            getNewMessage({
                'openid': openid
            }, {
                success: res => {
                    that.setData({
                        news: res.data
                    });
                }
            });
        });
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad();
        wx.stopPullDownRefresh();
    },

    /**
     * 跳转到资讯页面
     */
    more: function() {
        wx.switchTab({
            url: '../../pages/news/news',
        })
    },

    //跳转到资讯详情页面
    tiao_content: function (e) {
        wx.navigateTo({
            url: '../../pages/news_content/news_content?' + qs.stringify(e.currentTarget.dataset)
        })
    },

    /**
     * 跳转到免费咨询页面
     */
    tiao_free: function() {
        wx.navigateTo({
            url: '/pages/free/free'
        })
    },

    /**
     * 跳转到付费咨询页面
     */
    tiao_consult: function() {
        wx.navigateTo({
            url: '/pages/consult/consult'
        })
    },

    //跳转到我的消息页面
    tiao_myMessage: function () {
        wx.navigateTo({
            url: '../my_message/my_message'
        })
    },


    /**
     * 生成分享图
     */
    share: function() {
        this.setData({
            shareQuan: false,
        })
    },

    /**
     * 保存到相册
     */
    save: function() {
        var that = this;
        wx.saveImageToPhotosAlbum({
            filePath: 'images/shareimg_bg.jpg',
            success(errMsg) {
                wx.showModal({
                    content: '图片已保存到相册，赶紧晒一下吧~',
                    showCancel: false,
                    confirmText: '好哒',
                    confirmColor: '#72B9C3',
                });
            },
            fail() {
                wx.showToast({
                    title: '保存失败,请尝试重新提交',
                    icon: 'none',
                    duration: 1000
                });
            },
            complete() {
                that.setData({
                    shareQuan: true
                })
            }
        })
    },

    /**
     * 实现点击其它地方隐藏蒙层的效果
     */
    outbtn: function(e) {
        var that = this;
        that.setData({
            shareQuan: true
        })
    },

    /**
     * 防止事件冒泡，导致点击share_quan这里面的元素时，点击事件冒泡到share_quan触发它的slidedown事件
     */
    inbtn: function() {},

    //转发小程序函数
    onShareAppMessage: function(res) {
        return {
            title: '免费在线法律咨询',
            path: '/pages/index/index'
        }
    },

    //上拉触底事件
    onReachBottom() {
        //获取最新文章列表 + 翻页
        const last_page = this.data.article.last_page,
            current_page = this.data.article.current_page,
            per_page = this.data.article.per_page,
            that = this;
        if (current_page < last_page) {
            getArticleListNew({
                page: current_page + 1
            }, {
                success: res => {
                    const old = this.data.article.data,
                        news = res.data.data,
                        data = old.concat(news);
                    res.data.data = data;
                    that.setData({
                        article: res.data
                    });
                }
            });
        } else {
            //已经是最后一页了
            //console.log('已经是最后一页了');
        }
    }
})